package org.btik.light.mobile.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import org.btik.light.meta.DevArea;
import org.btik.light.mobile.R;
import org.btik.light.mobile.meta.mo.DefaultDevMo;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author lustre
 * @since 2023/4/28 17:15
 */
public class DevListAdapter extends ArrayAdapter<DefaultDevMo> {
    public DevListAdapter(@NonNull Context context, int resource, @NonNull @NotNull List<DefaultDevMo> objects) {
        super(context, resource, objects);
    }

    public static int getItemId() {
        return R.layout.dev_item;

    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        DefaultDevMo devMo = getItem(position);
        if (convertView == null || (viewHolder = (ViewHolder) convertView.getTag()) == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(getItemId(), parent, false);
            viewHolder = new ViewHolder(convertView);
            viewHolder.displayName = convertView.findViewById(R.id.display_name);
            viewHolder.sn = convertView.findViewById(R.id.sn);
            viewHolder.version = convertView.findViewById(R.id.version);
            viewHolder.devArea = convertView.findViewById(R.id.dev_area);
        }
        viewHolder.displayName.setText(devMo.getDisplayName());
        viewHolder.sn.setText(devMo.getSn());
        viewHolder.version.setText(devMo.getVersion());
        DevArea devArea = devMo.getDevArea();
        int imageId = devArea == DevArea.CLOUD ? R.drawable.cloud_service : R.drawable.local_area;
        viewHolder.devArea.setImageDrawable(ContextCompat.getDrawable(getContext(), imageId));

        return convertView;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView version;

        TextView sn;

        TextView displayName;

        ImageView devArea;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }

    }
}

